package newcoder_exp.bytedance;
import java.util.*;
import org.junit.*;

public class BD11 {
    

    //1.提供每个用户的登入登出时间，得到最高的登入时间峰值【线段覆盖问题】
    public int maxNumberOfPersons(int[][] nums) {
        int start, end, len = nums.length;
        int[] map = new int[24];
        for (int i = 0; i < len; i++) {
            start = nums[i][0];
            end = nums[i][1];
            for (int j = start; j <= end; j++) {
                map[j]++;
            }
        }
        int max = 0;
        for (int i = 0; i < 24; i++) {
            max = Math.max(max, map[i]);
        }
        return max;
    }

    public int overlapLine(int[][] lines) {
        class Node implements Comparable<Node> {
            int start;
            int end;
            public Node(int start, int end) {
                this.start = start;
                this.end = end;
            }
            public int compareTo(Node t) {
                if (this.end != t.end) return this.end - t.end;
                else return this.start - this.end;
            }
        }

        Queue<Node> deque = new PriorityQueue<>();
        for (int i = 0; i < lines.length; i++) {
            deque.offer(new Node(lines[i][0], lines[i][1]));
        }
        Node last = deque.poll(), cur;
        int ans = 1;
        while (!deque.isEmpty()) {
            cur = deque.poll();
            if (cur.start >= last.end) {
                ans++;
                last = new Node(last.start, cur.end); 
            }
        }
        return ans;
    }

    @Test
    public void test() {
        int[][] nums = {new int[]{0,2}, new int[]{2, 3}, new int[]{3,4}, new int[]{4,6}};
        System.out.println(overlapLine(nums));
    }
}
